new Vue({
    el: '#app',
    data() {
        return {
            form: {
                Calendar_Scheme_SerialNumber: '',
                Calendar_Scheme_Mode: '0',
                Enterprise_Info_SerialNumber: $.cookie('enterprise'),
                Children: {
                    calendar_scheme_detail: [{
                        Calendar_Scheme_Detail_SerialNumber: '',
                        Calendar_Scheme_SerialNumber: '',
                        Calendar_Scheme_Type: '0',
                        Calendar_Scheme_Reward: '',
                        Calendar_Scheme_Number: '',
                        Calendar_Scheme_Day: '',
                        Calendar_Scheme_Quantity: ''
                    }],
                    calendar_scheme_storescope: [{
                        Calendar_Scheme_Storescope_SerialNumber: '',
                        Calendar_Scheme_SerialNumber: '',
                        Store_SerialNumber: '',
                    }]
                }
            },
            loading: false,
            store: [],
            calendar_scheme_reward: [],
            Add: true//创建
        }
    },
    mounted() {
        this.getData();
    },
    methods: {
        getData() {//查询店铺
            const that = this;
            $.ajax({
                url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Data: 'store_info'
                },
                success: function (res) {
                    that.store = res
                },
                error: function () {
                    console.log(error)
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            }),
            $.ajax({
                url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Data: 'calendar_scheme_reward',
                    isNullEnterprise: true
                },
                success: function (res) {
                    that.calendar_scheme_reward = res
                },
                error: function () {
                    console.log(error)
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
            $.ajax({
                url: DomainName + '/getCalendarCofingAction?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Enterprise_Info_SerialNumber: $.cookie('enterprise')
                },
                success: function (res) {
                    if(res.hasOwnProperty("calendar_scheme")){
                        that.form = res.calendar_scheme[0]
                        that.Add = false
                    }
                },
                error: function () {
                    console.log(error)
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })

        },
        submitForm() {
            const _this = this
            this.loading = true
            if (this.Add) {
                $.ajax({
                    url: DomainName + '/CalendarCofingAction?token=' + $.cookie('token'),
                    type: 'POST',
                    data: {
                        Data: JSON.stringify(_this.form),
                    },
                    success: function (res) {
                        _this.$message.success("提交成功")
                        window.history.go(-1);
                    },
                    error: function () {
                        _this.$message.error("操作失败，请稍后再试")
                    },
                    complete: function (XMLHttpRequest) {
                        _this.loading = false
                        if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                        }
                    }
                })
            } else {
                const { add, modify } = _this.deal()
                let data = {}
                if (add == null) {
                    data = { Data: JSON.stringify({ Modify: { calendar_scheme: [modify] },Add:{}}) }
                } else {
                    data = {
                        Data: JSON.stringify({ Modify: { calendar_scheme: [modify] }, Add: { calendar_scheme: [add] }})
                    }
                }
                console.log(JSON.parse(data.Data));
                $.ajax({
                    url: DomainName + '/CalendarCofingAction?token=' + $.cookie('token'),
                    type: 'POST',
                    data,
                    success: function (res) {
                        _this.$message.success("提交成功")
                        window.history.go(-1);
                    },
                    error: function () {
                        _this.$message.error("操作失败，请稍后再试")
                    },
                    complete: function (XMLHttpRequest) {
                        _this.loading = false
                        if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                        }
                    }
                })
            }
        },
        deal() {
            const add = deepClone(this.form)
            const modify = deepClone(this.form)
            let add_calendar_scheme_detail = [], modify_calendar_scheme_detail = []
            let add_calendar_scheme_storescope = [], modify_calendar_scheme_storescope = []
            for (const i in modify.Children.calendar_scheme_detail) {
              const obj = modify.Children.calendar_scheme_detail[i];
              if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                add_calendar_scheme_detail.push(obj)
              } else {
                modify_calendar_scheme_detail.push(obj)
              }
            }
            for (const i in modify.Children.calendar_scheme_storescope) {
              const obj = modify.Children.calendar_scheme_storescope[i];
              if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                add_calendar_scheme_storescope.push(obj)
              } else {
                modify_calendar_scheme_storescope.push(obj)
              }
            }
      
            add.Children.calendar_scheme_detail = add_calendar_scheme_detail
            add.Children.calendar_scheme_storescope = add_calendar_scheme_storescope
      
            modify.Children.calendar_scheme_detail = modify_calendar_scheme_detail
            modify.Children.calendar_scheme_storescope = modify_calendar_scheme_storescope
      
            if (add_calendar_scheme_detail.length == 0 && add_calendar_scheme_storescope.length == 0) {
              return { add: null, modify };
            }
            return { add, modify };
          },
        add_calendar_scheme_detail() {
            this.form.Children.calendar_scheme_detail.push({
                Calendar_Scheme_Detail_SerialNumber: '',
                Calendar_Scheme_SerialNumber: '',
                Calendar_Scheme_Type: '',
                Calendar_Scheme_Reward: '',
                Calendar_Scheme_Number: '',
                Calendar_Scheme_Day: '',
                Calendar_Scheme_Quantity: ''
            })
        },
        remove_calendar_scheme_detail() {
            if (this.form.Children.calendar_scheme_detail.length > 1) {
                this.form.Children.calendar_scheme_detail.pop()
            } else {
                this.$message({
                    message: '不能在删除了',
                    type: 'warning'
                })
            }
        },
        add_calendar_scheme_storescope() {
            this.form.Children.calendar_scheme_storescope.push({
                Calendar_Scheme_Storescope_SerialNumber: '',
                Calendar_Scheme_SerialNumber: '',
                Store_SerialNumber: '',
            })
        },
        remove_calendar_scheme_storescope() {
            if (this.form.Children.calendar_scheme_storescope.length > 1) {
                this.form.Children.calendar_scheme_storescope.pop()
            } else {
                this.$message({
                    message: '不能在删除了',
                    type: 'warning'
                })
            }
        },

    }
})